package com.stephen.bp.hexin.controller;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.stephen.bp.hexin.entity.Category;
import com.stephen.bp.hexin.entity.Product;
import com.stephen.bp.hexin.model.ProductList;
import com.stephen.bp.hexin.service.CategoryService;
import com.stephen.bp.hexin.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Qiuww
 */
@RestController
@RequestMapping("/")
public class HexinController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductService productService;

    @GetMapping("/list")
    public List<ProductList> productList(Integer id) {
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (id != null) {
            categoryLambdaQueryWrapper.eq(Category::getId, id);
        }
        List<Category> list = categoryService.list(categoryLambdaQueryWrapper);
        List<ProductList> result = new ArrayList<>();
        for (Category category : list) {
            ProductList productList = new ProductList();
            productList.setId(category.getId());
            LambdaQueryWrapper<Product> productWrapperWrapper =  new LambdaQueryWrapper<>();
            productWrapperWrapper.eq(Product::getGoodsClassId, category.getId());
            productList.setChildren(productService.list(productWrapperWrapper));
            result.add(productList);
        }
        return result;
    }
}
